package blackstar.agent.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class PropertyConfig implements VariableStore
{
	private Properties defaults = new Properties();

	private VariableExpander expander = new VariableExpander(this, "${", "}");

	public void load(InputStream is) throws IOException
	{
		defaults.load(is);
	}

	public void loadFromXML(InputStream is)
			throws InvalidPropertiesFormatException, IOException
	{
		defaults.loadFromXML(is);
	}
	
	public void addProperty(String name, String value)
	{
		this.defaults.put(name, value);
	}
	
	public Properties getProperties()
	{
		return this.defaults;
	}

	public String getVariableValue(String field)
	{
		String preExpansion = this.defaults.getProperty(field);

		if (preExpansion == null)
		{
			return null;
		}

		return expander.expandVariables(preExpansion);
	}
}